﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class GuidedChecking : Form
    {
        public GuidedChecking()
        {
            InitializeComponent();
        }

        private void GuidedChecking_Load(object sender, EventArgs e)
        {
            dtpInceptionDate.Value = DateTime.Now.Date.AddMonths(-6);
            dtpFirstPaycheck.Value = DateTime.Now.Date.AddMonths(-6);
        }

        private bool ValidateForm()
        {
            string ErrorList = "";

            txtCheckingName.Text = txtCheckingName.Text.Trim();

            if (txtCheckingName.Text == "")
                ErrorList += "- You must enter a valid Account Name\n";
            else
            {
                if (MainForm.projector.FindFund(txtCheckingName.Text) != null)
                    ErrorList += "- A Fund already exists with that Account Name!\n";
            }

            try { Decimal IncepAmount = Decimal.Parse(txtInceptionAmount.Text); }
            catch { ErrorList += "- Inception Amount is not a valid amount\n"; }

            if (checkBox1.Checked)
            {
                try { Decimal PaycheckAmount = Decimal.Parse(txtPaycheckAmount.Text); }
                catch { ErrorList += "- Paycheck Amount is invalid\n"; }
            }

            if (ErrorList == "") return true;

            MessageBox.Show("Please correct the following errors :\n" + ErrorList, "Unable to create Checking Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        #region Button Handlers

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm()) return;

            Decimal IncepAmount = Decimal.Parse(txtInceptionAmount.Text);

            Fund fund = new Fund(
                new Balance(IncepAmount, dtpInceptionDate.Value, "Checking Inception"),
                txtCheckingName.Text, FundClassEnum.SpendingAccount, false);

            MainForm.projector.Funds.Add(fund);

            if (checkBox1.Checked)
            {
                Decimal PaycheckAmount = Decimal.Parse(txtPaycheckAmount.Text);
                Actor actor = new Actor("Paycheck Deposit", txtCheckingName.Text, 
                    ActorPeriodicityEnum.Weekly, rbWeekly.Checked?1:2, 
                    dtpFirstPaycheck.Value, PaycheckAmount);

                MainForm.projector.Actors.Add(actor);
            }

            MainForm.InstanceRef.HasChanged = true;

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Simple Numeric Textbox Filtering

        private void txtInceptionAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtPaycheckAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        #endregion

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/GuidedChecking.aspx";
            proc.Start();
        }
    }
}
